361C - Levko and Array Recovery - CodeForces Solution


brute force constructive algorithms greedy *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define int long long
#define ld long double

using namespace std;

struct Data {
    int t;
    int l;
    int r;
    int x;
};

int32_t main() {
    int n, m;
    cin >> n >> m;
    vector<Data> a(m);
    vector<int> res(n, -1e9);
    for (int i = 0; i < m; i++) {
        int t, l, r, x;
        cin >> t >> l >> r >> x;
        l--;
        r--;
        a[i].t = t;
        a[i].l = l;
        a[i].r = r;
        a[i].x = x;
        if (t == 2) {
            bool go = false;
            for (int j = l; j <= r; j++) {
                if (res[j] == -1e9 || res[j] >= x) {
                    res[j] = x;
                    go = true;
                }
            }
            if (!go) {
                cout << "NO";
                return 0;
            }
        } else {
            for (int j = l; j <= r; j++) {
                if (res[j] != -1e9) {
                    res[j] += x;
                }
            }
        }
    }
    for (int i = m - 1; i >= 0; i--) {
        if (a[i].t == 1) {
            for (int j = a[i].l; j <= a[i].r; j++) {
                res[j] -= a[i].x;
            }
        } else {
            int max1 = -1e9;
            for (int j = a[i].l; j <= a[i].r; j++) {
                max1 = max(max1, res[j]);
            }
            if (max1 != a[i].x) {
                cout << "NO";
                return 0;
            }
        }
    }
    cout << "YES" << '\n';
    for (int i : res) {
        if (i <= -1e9) {
            cout << 1000000000 << ' ';
        } else {
            cout << i << ' ';
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts